package com.shao.service.Impl;


import com.shao.bean.cls;
import com.shao.bean.teacher;
import com.shao.common.R;
import com.shao.common.addTeacherVo;
import com.shao.common.admTeacherInfo;
import com.shao.dao.teaDao;
import com.shao.service.teaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: com.song shao
 * @date: 2022/2/28
 * @time: 10:06
 * @version: 1.0
 */
@SuppressWarnings("all")
@Service
@Transactional
public class teaServiceImpl implements teaService {

    @Autowired
    private teaDao tea;


    /**
     * 管理员查询老师的信息
     *
     * @return
     */
    @Override
    public List<admTeacherInfo> admFindTeacherInfo() {
        List<admTeacherInfo> admTeacherInfos = new ArrayList<>();
        List<teacher> tas = tea.findTeacherInfos();                // 查询教师的信息
        for (teacher ta : tas) {
            admTeacherInfo admTeacherInfo = new admTeacherInfo();
            admTeacherInfo.setTea_username(ta.getTea_username());  // 设置账号
            admTeacherInfo.setTea_nikename(ta.getTea_nikename());  // 设置名字
            admTeacherInfo.setTea_state(ta.getTea_state());        // 设置账号的状态
            cls resu = tea.selectClass(ta.getTea_class());         // 返回的是一个班级
            admTeacherInfo.setClass_name(resu.getClass_name());    // 设置班级的名字
            admTeacherInfos.add(admTeacherInfo);                   // 返回班级
        }
        return admTeacherInfos;
    }

    /**
     * 添加账号
     *
     * @param adm
     * @return
     */
    @Override
    public boolean addTeacher(addTeacherVo adm) {
        String class_name = adm.getClass_name();
        String stu_nikename = adm.getStu_nikename();
        int stu_state = adm.getStu_state();
        String stu_username = adm.getStu_username();
        String tea_pwd = adm.getTea_pwd();
        if (!(stu_username != null && stu_username.length() == 11)) {
            return false;  // 账号
        }
        if (!(stu_nikename != null)) {
            return false;  // 昵称
        }
        if (!(class_name != null && tea_pwd != null)) {
            return false;  // 密码  班级
        }

        // 到达这里就说明数据全是对的
        List<cls> cs = tea.selectCls();
        cls cls1 = cs.get(cs.size() - 1);
        int class_id = cls1.getClass_id();
        int newClsName = class_id + 1;

        cls cls = new cls();
        cls.setClass_id(newClsName);
        cls.setClass_name(class_name);
        boolean addResu = tea.insetClass(cls); // 添加班级
        if (!addResu) {
            return false;
        }
        teacher teacher = new teacher();
        teacher.setTea_state(stu_state);
        teacher.setTea_nikename(stu_nikename);
        teacher.setTea_username(stu_username);
        teacher.setTea_class(newClsName);
        teacher.setTea_pwd(tea_pwd);
        teacher.setTea_code(1);
        boolean insetResule = tea.insertTeacher(teacher);
        return insetResule;
    }

    /**
     * 教师登录验证
     *
     * @param teacher
     * @return
     */
    @Override
    public R loginVerify(teacher teacher) {
        String tea_username = teacher.getTea_username();
        String tea_pwd = teacher.getTea_pwd();
        if (!(tea_username != null && tea_username.length() == 11 && tea_pwd.length() > 6)) {
            return R.error("请检查你的账号是否是（11位）,密码不能小于6位！");
        }
        teacher resu = tea.findByTeacher(tea_username);
        int tea_state = resu.getTea_state();
        if (tea_state == 1) {
            return R.error("你的账号目前属于封禁状态，无法登录，请联系管理员解封！");
        }
        String tea_pwd1 = resu.getTea_pwd();
        if (tea_pwd.equals(tea_pwd1)) {
            return R.success(222);
        }
        return R.error("你的账号或者密码不对，请重新输入");
    }
}
